package com.common.common.utils;

import android.app.Application;
import android.content.Context;

import androidx.annotation.NonNull;

import com.common.common.HttpClient;
import com.common.common.HttpConfig;

import java.io.File;

public class NetUtil<T> {
    private T mApiService;
    private Class<T> mClazz;

    public NetUtil(Class<T> clazz) {
        this.mClazz = clazz;
    }

    /**
     * 初始化HTTP设置
     */
    public void initHttpConfig(String baseUrl) {
        HttpConfig httpConfig = new HttpConfig()
                .setHttpCache(provideAppContext().getCacheDir())
                .setBaseUrl(baseUrl)
                .setOkHttpBuilder(null);//默认为url_name

        mApiService = HttpClient
                .getInstance(httpConfig)
                .getRetrofit(false)
                .create(mClazz);
    }

    public T getApiService() {
        return mApiService;
    }

    /**
     * 初始化HTTP设置
     */
    public void initHttpConfig(HttpConfig httpConfig) {
        mApiService = HttpClient
                .getInstance(httpConfig)
                .getRetrofit(false)
                .create(mClazz);
    }

    private static Application sApplication;

    public static void init(@NonNull final Context context) {
        sApplication = (Application) context.getApplicationContext();
    }

    public static Context provideAppContext() {
        if (sApplication != null) {
            return sApplication;
        } else {
            throw new NullPointerException("Common module application is null!");
        }
    }
}
